package com.taojin.shoppinginterest.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.taojin.shoppinginterest.R;
import com.taojin.shoppinginterest.adapter.AutoScrollAdapter;
import com.taojin.shoppinginterest.adapter.SelectionFragmentAdapter;
import com.taojin.shoppinginterest.bean.Poster;
import com.taojin.shoppinginterest.bean.Selection;
import com.taojin.shoppinginterest.bean.ShopList;
import com.taojin.shoppinginterest.constants.ApiConfig;
import com.taojin.shoppinginterest.widget.AutoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by T400 on 15-5-11.
 */
public class SelectionFragment extends Fragment implements PullToRefreshBase.OnRefreshListener2{
    @ViewInject(R.id.pull_to_refresh_listview_select)
    private PullToRefreshListView pullToRefreshListView;
    private AutoScrollViewPager autoScrollViewPager;
    private LinearLayout dotLayout;
    private List<ShopList> list;
    private SelectionFragmentAdapter selectionFragmentAdapter;
    private HttpUtils httpUtils;
    private AutoScrollAdapter autoPageAdapter;
    private List<Poster> urlList;
    private View headView;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.activity_selectionfragment,container,false);
        ViewUtils.inject(this,view);
        httpUtils=new HttpUtils();
        initData(inflater);
        loadData();
        initViewPager();
        loadViewPagerData();
        return view;
    }

    private  View getHeadView(LayoutInflater inflater){
        headView=inflater.inflate(R.layout.headview_select_pulltorefreshlistview,null);
        return headView;
    }

    private void initViewPager() {
        autoScrollViewPager= (AutoScrollViewPager) headView.findViewById(R.id.auto_scroll_viewpager);
        dotLayout= (LinearLayout) headView.findViewById(R.id.layout_banner_dots);
        urlList=new ArrayList<Poster>();
        autoPageAdapter=new AutoScrollAdapter(getActivity(),urlList,dotLayout);
        autoScrollViewPager.setOnPageChangeListener(autoPageAdapter);
        autoScrollViewPager.setAdapter(autoPageAdapter);
    }

    private void loadViewPagerData() {
        RequestParams requestParams=new RequestParams();
        requestParams.addBodyParameter("act","home");
        requestParams.addBodyParameter("app", "other");
        requestParams.addBodyParameter("lat","32.03789");
        requestParams.addBodyParameter("lon", "118.77615");
        requestParams.addBodyParameter("pt", "2");
        requestParams.addBodyParameter("ver", "1.1");
        httpUtils.send(HttpRequest.HttpMethod.POST, ApiConfig.URL, requestParams, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String data = responseInfo.result;
                Selection selection = JSON.parseObject(data, Selection.class);
                if (selection != null) {
                    if (selection.getData() != null) {
                        if (selection.getData().getPoster() != null && !selection.getData().getPoster().isEmpty()) {
                            urlList.addAll(selection.getData().getPoster());
                            autoPageAdapter.notifyDataSetChanged();

                        }
                    }

                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(getActivity(), "当前网络有误，加载失败", Toast.LENGTH_SHORT).show();
            }
        });

    }
    private void initData(LayoutInflater inflater) {
        list=new ArrayList<ShopList>();
        selectionFragmentAdapter=new SelectionFragmentAdapter(list,getActivity());
        ListView mListView=pullToRefreshListView.getRefreshableView();
        getHeadView(inflater);
        mListView.addHeaderView(headView);
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        pullToRefreshListView.setOnRefreshListener(this);
        pullToRefreshListView.setAdapter(selectionFragmentAdapter);

    }
    private void loadData() {
        RequestParams requestParams=new RequestParams();
        requestParams.addBodyParameter("act","home");
        requestParams.addBodyParameter("app","other");
        requestParams.addBodyParameter("lat","32.03789");
        requestParams.addBodyParameter("lon","118.77615");
        requestParams.addBodyParameter("pt","2");
        requestParams.addBodyParameter("ver","1.1");
        httpUtils.send(HttpRequest.HttpMethod.POST, ApiConfig.URL, requestParams, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String data=responseInfo.result;
                Selection selection= JSON.parseObject(data,Selection.class);
                if(selection!=null){
                    if(selection.getData()!=null){
                        if(selection.getData().getShop_list()!=null&&!selection.getData().getShop_list().isEmpty()){
                        list.addAll(selection.getData().getShop_list());
                        selectionFragmentAdapter.notifyDataSetChanged();
                            pullToRefreshListView.onRefreshComplete();

                        }
                    }

                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(getActivity(),"当前网络有误，加载失败",Toast.LENGTH_SHORT).show();
            }
        });

    }
    @Override
    public void onResume() {
        super.onResume();
        autoScrollViewPager.startAutoScroll();
    }

    @Override
    public void onPause() {
        super.onPause();
        autoScrollViewPager.stopAutoScroll();
    }


    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        loadData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {

    }
}
